/*
* 路由对象模块
* */
import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from './utils/token'

/*引入pages*/
const Login = ()=>import('./views/Login');
const Index = ()=>import('./views/Index');
const Table = ()=>import('./views/Table');

//申明使用插件
Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    {
      path: '/',
      redirect: '/login',
    },
    {
      path:'/login',
      component: Login,
      meta: {
        title: "登录"
      }
    },
    {
      path:'/index',
      component: Index,
      name: 'index',
      meta: {
        title: "首页",
        isAlive: true
      }
    },
    {
        path: '/table',
        name: 'table',
        component: Table,
        meta: {
            title: "清单"
        }
    }
  ]
})
router.beforeEach((to,from,next) => {
  let token = getToken()
  if(to.meta && to.meta.title) {
      document.title = to.meta.title
  }
  if(token) {
    next()
  }else {
    if(to.path == '/login') {
      next()
    }else {
      next({
        path: '/login'
      })
    }
  }
})
export default router